﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class CubExplode : MonoBehaviour
{
    [Header("Y轴向外平面")]
    public GameObject[] planeYOut;

    [Header("Y轴向内平面")]
    public GameObject[] planeYIn;

    [Header("炸开速度")]
    public float speed = 120f;

    [Header("默认透明度")]
    public float alphaCube = 20f;

    [Header("默认缩放")]
    public float scaleCube = 1f;

    [Header("默认位置")]
    public float positionCube = 10f;

    [Header("目标透明度")]
    public float alphaTag = 255f;

    [Header("目标缩放")]
    public float scaleTag = 2f;

    [Header("目标位置")]
    public float positionTag = 20f;

    private Renderer[] renderersPlane;
    private Vector3[] positionPlane;

    private float alphaC, scaleC, positionC;

    public bool isExplode;

    private float progress;

    /// <summary>
    /// 其他程序调用接口
    /// </summary>
    public void Touch()
    {
            isExplode = !isExplode;
    }

    void Start()
    {
        isExplode = false;
        renderersPlane = new Renderer[planeYIn.Length + planeYOut.Length];
        positionPlane = new Vector3[planeYOut.Length];

        alphaC = alphaCube;
        scaleC = scaleCube;
        positionC = positionCube;

        progress = 0;

        //获取所有的Renderer组件
        int i = 0;
        for (; i < planeYOut.Length; i++)
        {
            renderersPlane[i] = planeYOut[i].GetComponent<Renderer>();
            positionPlane[i] = planeYOut[i].transform.localPosition;
        }
        for (; i < renderersPlane.Length; i++)
        {
            renderersPlane[i] = planeYIn[i - planeYOut.Length].GetComponent<Renderer>();
        }
    }
    void FixedUpdate()
    {
            ChangeAlpha();
            ChangeScale();
            ChangePosition();
            Explode();
    }
    void Update()
    {
        //RayTest();
    }
        /// <summary>
        /// 射线检测，后面还是觉得点开比较好
        /// </summary>
    void RayTest()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isExplode = !isExplode;
        }
    }
    /// <summary>
    /// 炸开逻辑
    /// </summary>
    void Explode()
    {
        if (isExplode)
        { 
            progress += speed * 0.01f * Time.deltaTime;
            /*
                        alphaC = Mathf.Lerp(alphaCube, alphaTag, progress);
                        scaleC = Mathf.Lerp(scaleCube, scaleTag, progress);
                        positionC = Mathf.Lerp(positionCube, positionTag, progress);
            */
            if (progress > 1)
            {
                progress = 1;
            }
        }
        else
        {
            progress -= speed * 0.01f * Time.deltaTime;
            if(progress < 0)
            {
                progress = 0;
            }
        }

        alphaC = Mathf.Lerp(alphaCube, alphaTag, progress);
        scaleC = Mathf.Lerp(scaleCube, scaleTag, progress);
        positionC = Mathf.Lerp(positionCube, positionTag, progress);
        /*
        alphaC = Mathf.Lerp(alphaTag, positionCube, progress);
        scaleC = Mathf.Lerp(scaleTag, positionCube, progress);
        positionC = Mathf.Lerp(positionTag, positionCube, progress);
        */
    }
    /// <summary>
    /// 更改透明度
    /// </summary>
    void ChangeAlpha()
    {
        if (alphaC > 255)
        {
            alphaC = 255;
        }

        if (alphaC < 0)
        {
            alphaC = 0;
        }

        float tag = alphaC / 255f;
        foreach (Renderer rendererChange in renderersPlane)
        {
            Color color = rendererChange.material.color;
            color.a = tag;
            rendererChange.material.color = color;
        }
    }
    /// <summary>
    /// 更改缩放
    /// </summary>
    void ChangeScale()
    {
        /*
        foreach (GameObject obj in planeYIn)
        {
            obj.transform.localScale = new Vector3(scaleC, scaleC, scaleC);
        }
        */
        foreach (GameObject obj in planeYOut)
        {
            obj.transform.localScale = new Vector3(scaleC, scaleC, scaleC);
        }
    }
    /// <summary>
    /// 更改坐标
    /// </summary>
    void ChangePosition()
    {
        int i = 0;
        for (; i < planeYOut.Length; i++)
        {
            positionPlane[i] = positionC * planeYOut[i].transform.up;
            planeYOut[i].transform.position = positionPlane[i];
        }
    }
}